<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="logo" />
        <span>电商系统Demo版</span>
      </div>
      <el-dropdown trigger="click">
        <span class="el-dropdown-link">
          <i class="iconfont icon-user"></i>admin
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item divided icon='el-icon-setting'>设置</el-dropdown-item>
          <el-dropdown-item @click.native="logout" divided icon='el-icon-s-promotion'>退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside class="asideLeft">
        <!-- 侧边栏菜单 -->
        <!-- 参数说明：
        router: 开启后配合index跳转使用 -->
        <el-menu
          background-color="slategray"
          text-color="#f3f3f3"
          active-text-color="#409EFF"
          :default-active="activeItem"
          class="el-menu-vertical"
          :collapse="isCollapse"
          :collapse-transition='false'
          :unique-opened="true"
          :router="true"
        >
         <!-- <el-button v-model="isCollapse" style="margin-bottom: 20px;" @click="handleCollapse">点击
        </el-button> -->
          <div class="handleCollapseIcon" @click="handleCollapse">
            <i v-show='isCollapse' class="el-icon-s-unfold"></i>
            <i v-show='!isCollapse' class="el-icon-s-fold"></i>
          </div>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key='item.id'>
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="_item.path" v-for="_item in item.children" :key='_item.id' @click="saveActiveMenuItem(_item.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{_item.authName}}</span>
              </template></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main :v-loading='loading'>
          <transition name="fade-transform" mode="out-in" tag='div'>
            <!-- <router-view></router-view> -->
            <router-view/>
          </transition>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenus } from '@/utils/https.js'
export default {
  components: {
    // Report
  },
  created() {
    this.getMenuList()
  },
  data() {
    return {
      isLoad: true,
      menuList: [],
      isCollapse: false,
      activeItem: '',
      iconList: {
        101: 'iconfont icon-user', // 用户管理
        103: 'iconfont icon-id-card', // 权限管理
        106: 'iconfont icon-statistics', // 商品管理
        110: 'iconfont icon-edit', // 订单管理
        112: 'iconfont icon-count' // 数据统计
      }
    }
  },
  computed: {
    loading() {
      return this.isLoad
    }
  },
  methods: {
    // 保存二级菜单激活状态
    saveActiveMenuItem(activeItem) {
      window.sessionStorage.setItem('activeItem', activeItem)
      this.activeItem = activeItem
    },

    // 处理菜单折叠
    handleCollapse() {
      this.isCollapse = !this.isCollapse
    },

    // 退出登录
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },

    // 获取菜单数据
    async getMenuList() {
      const { data: res } = await getMenus()
      if (res.data.status !== 200) return this.$message.error('菜单获取失败')
      this.isLoad = false
      this.activeItem = window.sessionStorage.getItem('activeItem') || '/welcome'
      this.menuList = res.data.menu
    }
  }
}
</script>

<style lang="less" scoped>
@import '../assets/css/const.less';
// elementUI中的标签名字就是css的className
.el-container {
  height: 100%;
}
.el-header {
  background-color: black;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
    img {
      width: 55px;
      height: 55px;
    }
    .el-dropdown-link {
      display: flex;
      justify-content: center;
      cursor: pointer;
      color: @niceBlue;
      font-size: 16px;
      .iconfont.icon-user{
        display: flex;
        justify-content: center;
        font-size: 16px;
        color: @niceBlue;
        margin: auto;
        margin-right: 10px;
      }
    }
  }
  .el-button {
    background-color: @niceBlue;
  }
}
.el-aside {
  .el-menu{
    border: none;
  }
  // 宽度内容自适应
  width:fit-content !important;
  font-size: 26px;
  /deep/.el-submenu{
    .el-submenu__title{
      display: flex;
      align-items: center;
      font-size: 16px;
      i{
        color: @niceWhite !important;
      }
    }
    .el-submenu__title:hover{
      // 轮廓
      outline: 0 !important;
      background-color: silver!important;
    }
    .el-menu-item:hover{
      // 轮廓
      outline: 0 !important;
      background-color: @niceSliver!important;
    }
  }
  .el-icon-menu{
    color: @niceWhite;
  }
  .el-menu-item.is-active{
    .el-icon-menu{
      color: @niceBlue;
    }
  }
  .iconfont{
    margin-right: 18px;
    font-size: 26px;
    color: @niceWhite;
  }
  .el-submenu__title i{
    color: @niceWhite;
  }
}
.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
  min-height: 100%;
  background-color: slategray;
}
.el-menu--collapse:not(.el-menu--vertical) {
  height: 100%;
}
.el-main {
  background-color: #fff;
}
.handleCollapseIcon{
  height: 56px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-bottom: 2px solid silver;
  > i{
    color: silver;
    display: flex;
  }
}
</style>
